package com.vini.web.panels;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.vini.core.ec.Circles;
import com.vini.core.ec.Company;
import com.vini.core.ec.Travel;
//import com.vini.core.ec.Travel;
import com.vini.core.ec.User;
import com.vini.core.si.CircleService;
import com.vini.core.si.CompanyService;
//import com.vini.core.si.TravelService;
import com.vini.core.si.UserService;
import com.vini.web.AppSession;
import com.vini.web.pages.Home;
import com.vini.web.pages.SignUp;

public class IndividualPanel extends Panel {
	private static final long serialVersionUID = 1L;

	private Form<User> userForm;
	private RequiredTextField<String> fName;
	private RequiredTextField<String> lName;
	private RequiredTextField<String> addressLine1;
	private RequiredTextField<String> addressLine2;
	private RequiredTextField<String> city;
	private RequiredTextField<String> state;
	private RequiredTextField<String> mobile;
	private DropDownChoice<Company> company;
	private RequiredTextField<String> locality;
	private ListMultipleChoice<Circles> circles;
	private RequiredTextField<String> birthdate;
	private RadioChoice<String> gender;
	private RequiredTextField<String> email;
	private PasswordTextField password;
	private PasswordTextField confirmPassword;
	private ListMultipleChoice<Travel> travel;
	private AjaxSubmitLink register;
	private AjaxLink<String> addVehicles;
	private ModalWindow vehicleWindow;
	private ModalWindow modalWindow;

	@SpringBean
	private UserService userService;
	@SpringBean
	private CompanyService companyService;
	@SpringBean
	private CircleService circleService;
	//@SpringBean
	//private TravelService travelService;


	public IndividualPanel(String id) {
		super(id);
		createCompanyWindow();
		createVehicleWindow();
		ChoiceRenderer<Company> compRenderer = new ChoiceRenderer<Company>("name");
		ChoiceRenderer<Circles> circleRenderer = new ChoiceRenderer<Circles>("name");
		ChoiceRenderer<Travel> travelRenderer = new ChoiceRenderer<Travel>("travelType");

		userForm = new Form<User>("userForm", new CompoundPropertyModel<User>(new User()));
		fName = new RequiredTextField<String>("firstName");
		lName = new RequiredTextField<String>("lastName");
		addressLine1 = new RequiredTextField<String>("line1");
		addressLine2 = new RequiredTextField<String>("line2");
		city = new RequiredTextField<String>("city");
		state = new RequiredTextField<String>("state");
		mobile = new RequiredTextField<String>("mobileNo");

		company = new DropDownChoice<Company>("company", getCompanies(), compRenderer);
		company.setNullValid(false);
		company.add(new AjaxFormComponentUpdatingBehavior("onchange") {

			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				if (company.getModelObject().getName().equals("New")) {
					info("Please Register New Company.");
					modalWindow.show(target);
				}
			}
		});

		locality = new RequiredTextField<String>("locality");
		circles = new ListMultipleChoice<Circles>("circles", getCircles(), circleRenderer);
		birthdate = new RequiredTextField<String>("birthdate");
		gender = new RadioChoice<String>("gender", Arrays.asList(new String[]{"Male","Female"})).setSuffix("");
		email = new RequiredTextField<String>("emailId");
		password = new PasswordTextField("password");
		confirmPassword = new PasswordTextField("confirm", Model.of(""));
		travel = new ListMultipleChoice<Travel>("travels", Travel.getTravels(), travelRenderer);

		addVehicles = new AjaxLink<String>("addVehicle") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				target.appendJavaScript("Wicket.Window.unloadConfirmation = false;");
				vehicleWindow.show(target);
			}
		};

		register = new AjaxSubmitLink("register") {
			private static final long serialVersionUID = 1L;
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				if (!userForm.getModelObject().getPassword().equals(confirmPassword.getModelObject())) {
					error("Password does not matches. Please Retype.");
					modalWindow.show(target);
				}else{
					User user = saveUser(userForm.getModelObject());
					user.setVehicles(((AppSession)getSession()).getVehicles());
					user = saveUser(user);
					if (user.getId() != null) {
						info("Registration Successfull. Redirecting to Home Page");
						((AppSession)getSession()).setUser(user);
						target.appendJavaScript("Wicket.Window.unloadConfirmation = false;");
						modalWindow.setCloseButtonCallback(new ModalWindow.CloseButtonCallback() {  
							private static final long serialVersionUID = 1L;

							public boolean onCloseButtonClicked(AjaxRequestTarget target) { 
								setResponsePage(Home.class);
								return true;
							}  
						}); 
						modalWindow.show(target);
					}else{
						error("Error While Registering User.");
						modalWindow.show(target);
					}
				}
			}
		};
		userForm.add(fName);
		userForm.add(lName);
		userForm.add(addressLine1);
		userForm.add(addressLine2);
		userForm.add(city);
		userForm.add(state);
		userForm.add(mobile);
		userForm.add(locality);
		userForm.add(circles);
		userForm.add(birthdate);
		userForm.add(gender);
		userForm.add(company);
		userForm.add(email);
		userForm.add(password);
		userForm.add(confirmPassword);
		userForm.add(travel);
		userForm.add(addVehicles);
		userForm.add(register);
		add(userForm);
		add(modalWindow);
		add(vehicleWindow);
	}


	private List<Circles> getCircles() {
		return circleService.getCircles();
	}

	private List<Company> getCompanies() {
		List<Company> companies = companyService.getAllCompanies();
		if (companies == null) {
			companies = new ArrayList<Company>();
		}
		Company newComp = new Company();
		newComp.setName("New");
		companies.add(newComp);
		return companies;
	}

	private User saveUser(User user1) {
		return userService.saveUser(userForm.getModelObject());
	}

	private ModalWindow createCompanyWindow() {
		modalWindow = new ModalWindow("newCompany");
		modalWindow.setTitle(Model.of("Message"));
		modalWindow.setOutputMarkupId(true);
		modalWindow.setInitialHeight(100);
		modalWindow.setInitialWidth(500);
		FeedbackPanel panel = new FeedbackPanel(modalWindow.getContentId());
		modalWindow.setContent(panel);

		return modalWindow;
	}

	private ModalWindow createVehicleWindow() {
		vehicleWindow = new ModalWindow("vehicle");
		vehicleWindow.setTitle(Model.of("Upload Vehicles"));
		vehicleWindow.setOutputMarkupId(true);
		vehicleWindow.setInitialHeight(200);
		vehicleWindow.setInitialWidth(500);
		VehiclePanel vehiclePanel = new VehiclePanel(vehicleWindow.getContentId());
		vehicleWindow.setContent(vehiclePanel);

		vehicleWindow.setCloseButtonCallback(new ModalWindow.CloseButtonCallback() {  
			private static final long serialVersionUID = 1L;

			public boolean onCloseButtonClicked(AjaxRequestTarget target) {  
				setResponsePage(SignUp.class);
				return true;
			}  
		});  

		return vehicleWindow;
	}

}
